package net.zoneland.knowledge.dao;

import java.util.List;
import net.zoneland.knowledge.model.Role;
import net.zoneland.knowledge.model.RoleExample;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface RoleMapper {
    @SelectProvider(type=RoleSqlProvider.class, method="countByExample")
    long countByExample(RoleExample example);

    @Insert({
        "insert into AITK_ROLE_INFO (ID, ROLEID, ",
        "ROLENAME, DESCRIPTION, ",
        "STATUS, TYPE, CREATETIME)",
        "values (#{id,jdbcType=VARCHAR}, #{roleId,jdbcType=VARCHAR}, ",
        "#{roleName,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, ",
        "#{status,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})"
    })
    int insert(Role record);

    @InsertProvider(type=RoleSqlProvider.class, method="insertSelective")
    int insertSelective(Role record);

    @SelectProvider(type=RoleSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="ID", property="id", jdbcType=JdbcType.VARCHAR),
        @Result(column="ROLEID", property="roleId", jdbcType=JdbcType.VARCHAR),
        @Result(column="ROLENAME", property="roleName", jdbcType=JdbcType.VARCHAR),
        @Result(column="DESCRIPTION", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="STATUS", property="status", jdbcType=JdbcType.VARCHAR),
        @Result(column="TYPE", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="CREATETIME", property="createTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Role> selectByExample(RoleExample example);

    @UpdateProvider(type=RoleSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") Role record, @Param("example") RoleExample example);

    @UpdateProvider(type=RoleSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") Role record, @Param("example") RoleExample example);

    @Select(" select ID, ROLEID, ROLENAME, DESCRIPTION,STATUS, TYPE, CREATETIME " +
            " from  AITK_ROLE_INFO  where roleId in (select  roleid from aitk_user_role_rel where userid = #{userId}) ")
    @Results({
            @Result(column="ID", property="id", jdbcType=JdbcType.VARCHAR),
            @Result(column="ROLEID", property="roleId", jdbcType=JdbcType.VARCHAR),
            @Result(column="ROLENAME", property="roleName", jdbcType=JdbcType.VARCHAR),
            @Result(column="DESCRIPTION", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="STATUS", property="status", jdbcType=JdbcType.VARCHAR),
            @Result(column="TYPE", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="CREATETIME", property="createTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Role> getByUserId(@Param("userId") final String userId);


    /**
     * 通过用户id查询角色信息列表.
     * @param userId 用户id.
     * @return 返回角色信息列表.
     */
    @Select(" select roleid from aitk_user_role_rel where userid = #{userId}")
    List<String> getRoleIdByUserId(@Param("userId") final String userId);
}
